﻿#include <CommonHelper.h>
#include <QDebug>

CommonHelper* CommonHelper::Instance=0;

CommonHelper* CommonHelper::commonInstance()
{
    if(Instance==0)
        Instance =new CommonHelper();
    return Instance;
}

CommonHelper::CommonHelper()
{
}

QString CommonHelper::GetGuid(){
    QUuid id = QUuid::createUuid();
    QString strGuid =id.toString(QUuid::Id128);
    return  strGuid;
}

QString CommonHelper::GetDateTimeStr(){
    QDateTime current=QDateTime::currentDateTime();
    QString dateStr=current.toString("yyyy-MM-dd hh:mm:ss");
    return dateStr;

}

QString CommonHelper::TenToSixteen(int num, int bit){
    QString hex = QString::number(num, 16);
    if (hex.length() < bit) {
        int n = bit - hex.length();
        for (int i = 0; i < n; i++) {
            hex.prepend("0");
        }
    }

    QString result;
    for (int i = 0; i < hex.length(); ++i) {
        result.append(hex.at(i));
        if (i % 2 == 1 && i != hex.length() - 1) {
            result.append(" ");
        }
    }
    return result;
}

//2进制转16进制 参数 例:11111111
QString CommonHelper::binaryToHex(const QString &data) {
    bool ok;
    int val = data.toInt(&ok,2);
    QString m_strHex = QString("%1").arg(val,2,16,QLatin1Char('0')).toUpper();
    return m_strHex;
}

QByteArray CommonHelper::GetSendData(QString sendData){
    sendData= sendData.trimmed();
    QByteArray result;
    for(int i = 0;i<sendData.size();i++)
    {
        if(sendData[i]==' ')
            continue;   //跳过
        int num  = sendData.mid(i,2).toUInt(nullptr,16);
        i++;
        result.append(num);
    }
    return result;
}

QString CommonHelper::GetCrcCheck(QString data) {
    QByteArray dataArray = hexStringToByteArray(data);

    int len=dataArray.size();
    uint16_t wcrc=0XFFFF;//预置16位crc寄存器，初值全部为1
    uint8_t temp;//定义中间变量
    int i=0,j=0;//定义计数
    for(i=0;i<len;i++)//循环计算每个数据
    {
        temp=dataArray.at(i);
        wcrc^=temp;
        for(j=0;j<8;j++){
            //判断右移出的是不是1，如果是1则与多项式进行异或。z
            if(wcrc&0X0001){
                wcrc>>=1;//先将数据右移一位
                wcrc^=0XA001;//与上面的多项式进行异或
            }
            else//如果不是1，则直接移出
                wcrc>>=1;//直接移出
        }
    }
    return TenToSixteen(uint8_t(wcrc>>8),1)+" "+TenToSixteen(uint8_t(wcrc),1);
}

QString CommonHelper::SixteenToTen(QString data){
    data.replace(" ","");
    bool ok;
    int dec = data.toInt(&ok,16);
    QString num = QString("%1").arg(dec,0,10);
    return  num;
}

QString CommonHelper::SixteenToTwo(QString data){
    QString bin = QString::number(data.toInt(NULL, 16), 2);
    return bin;
}

QByteArray CommonHelper::hexStringToByteArray(QString HexString)
{
    bool ok;
    QByteArray ret;
    HexString = HexString.trimmed();
    HexString = HexString.simplified();
    QStringList sl = HexString.split(" ");

    foreach (QString s, sl)
    {
        if(!s.isEmpty())
        {
            char c = static_cast<char>(s.toInt(&ok, 16) & 0xFF);
            if(ok)
            {
                ret.append(c);
            }
            else
            {
                qDebug()<<"error:"<<s;

            }
        }
    }

    return ret;
}

QString CommonHelper::getItemCode(QString id,QString name){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select num From Deftab Where id='%1' and value='%2'").arg(id,name);
    query.exec(sql);
    QString result;
    while (query.next()) {
        result= query.value("num").toString();
    }
    return  result;
}
QString CommonHelper::getItemName(QString id,QString code)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value From Deftab Where id='%1' and num='%2'").arg(id,code);
    query.exec(sql);
    QString result;
    while (query.next()) {
        result= query.value("value").toString();
    }
    return  result;
}

QString CommonHelper::minToSecond(QString min,QString diameter)
{
    double r=diameter.toDouble()/2;
    double round=3.1415*r*r;
    double result=min.toDouble()*1000/(60*round);
    qDebug()<<QString::number(result,'f',6);
    return QString::number(result,'f',6);
}

QString CommonHelper::secondToMin(QString sec,QString diameter)
{
    double r=diameter.toDouble()/2;
    double round=3.1415*r*r;
    double result=(60*round*sec.toDouble())/1000;
    return QString::number(result,'f',6);
}


double CommonHelper::minToSecond(double min,double diameter)
{
    double r=diameter/2;
    double round = 3.1415 * r * r;
    double result = min * 1000 / (60*round);
    return result;
}

double CommonHelper::secondToMin(double sec,double diameter)
{
    double r = diameter / 2;
    double round = 3.1415 * r * r;
    double result = (60 * round * sec) / 1000;
    return result;
}

QStringList CommonHelper::getErrorInfo(QString module,QString errorCode)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select ErrorName,ErrorMethod,IsBlocked From FaultInformation Where Module='%1' and ErrorCode='%2'").arg(module,errorCode);
    qDebug()<<sql;
    query.exec(sql);
    QStringList result;
    QString isBlocked;
    while (query.next()) {
        result.append(query.value("ErrorName").toString());
        result.append(query.value("ErrorMethod").toString());
        isBlocked=query.value("IsBlocked").toString();
    }
    if(isBlocked=="是"){
        result.clear();
    }
    return  result;
}
